package cn.admin.config.common.protocol;

import com.caucho.hessian.io.AbstractDeserializer;
import com.caucho.hessian.io.AbstractHessianInput;

import java.io.IOException;

class SwizzleDecoder extends AbstractDeserializer {

    @Override
    public Class<?> getType() {
        return Swizzle.class;
    }

    @Override
    public Object readMap(AbstractHessianInput in) throws IOException {
        int serialization = 0;
        byte[] bytes = null;

        for(int i = 0; i < 2; ++i) {
            String key = in.readString();
            if ("serialization".equals(key)) {
                serialization = in.readInt();
            } else if ("bytes".equals(key)) {
                bytes = in.readBytes();
            }
        }

        in.readMapEnd();
        Swizzle swizzle = new Swizzle(null);
        try {
            SwizzleCodec.f_bytes.set(swizzle,bytes);
            SwizzleCodec.f_serialization.set(swizzle,serialization);
        } catch (IllegalAccessException e) {
            throw new IOException(e);
        }
        in.addRef(swizzle);
        return swizzle;
    }
}
